/****************************************************************************

	COPYRIGHT(C) MAXSI SOFTWARE, JONAS 'SORTIE' TERMANSEN 2008, 2009, 2010

    This file is part of Maxsi Installer.

    Maxsi Installer is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Maxsi Installer is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with Maxsi Installer.  If not, see <http://www.gnu.org/licenses/>.

	*/ #include "MaxsiInstallerContributors.h" /*

	If you modify this file, please enter your name below and provide contact
	information in MaxsiEngineContributors.h. For more information please see
	MaxsiEngineContributors.h.
	
	Contributors to this file:

	- Jonas 'Sortie' Termansen
	- [your name here]

	MaxsiInstaller.exe
	The main installing technology utilized in the powerful Maxsi Distribution
	platform.

	PageInstaller.cpp
	IT'S ALIVE! - Erhm, installing!

****************************************************************************/

#include "MaxsiEngine.h"
#include "MaxsiDistributionLink.h"
#include "Main.h" 
#include "UI.h" 
#include "Thread.h"
#include "Data.h"

bool PageInstaller::SetProgress(int Progress)
{
	if (!Initialized) { return false; }
	if (!ProgressBar) { return false; }

	SendMessage(ProgressBar,PBM_SETPOS,(WPARAM)Progress,(LPARAM)0);

	return true;
}


bool PageInstaller::InitializeForm(ME_Window* Parent)
{
	if ( IsUpdate )
	{
		HeaderTextOverride = BuildString(3,"Automatically Updating ",ProductName,"!");
	}
	else
	{
		HeaderTextOverride = BuildString(3,"Installing ",ProductName,"!");
	}

	BaseClass::InitializeForm(Parent);

	delete[] HeaderTextOverride; HeaderTextOverride = 0;

	RECT ClientSpace;
	GetClientRect (WindowHWND, &ClientSpace);

	ProgressBar		= CreateProgressBarA(Parent->WindowHWND,0,0,0,0,0);
	StatusLabel		= CreateLabelW		(Parent->WindowHWND,0,0,0,0,0);
	SubStatusLabel	= CreateLabelW		(Parent->WindowHWND,0,0,0,0,0);
	cmdStats		= CreateButtonA		(Parent->WindowHWND,"Stats >",0,0,0,0);
	cmdNews			= CreateButtonA		(Parent->WindowHWND,"News >",0,0,0,0);
	cmdError		= CreateButtonA		(Parent->WindowHWND,"Errors >",0,0,0,0);	
	txtNews			= CreateTextExA		(Parent->WindowHWND,"Please wait while connecting...",0,0,0,0,WS_VISIBLE | WS_CHILD | ES_LEFT | WS_VSCROLL | ES_AUTOVSCROLL | ES_MULTILINE);
	txtError		= CreateTextExA		(Parent->WindowHWND,"",0,0,0,0,WS_VISIBLE | WS_CHILD | ES_LEFT | WS_VSCROLL | ES_AUTOVSCROLL | ES_MULTILINE);

	ShowWindow(SubStatusLabel,SW_SHOW);
	ShowWindow(txtNews,SW_HIDE);
	ShowWindow(txtError,SW_HIDE);

	NewResolution((WORD)ClientSpace.right,(WORD)ClientSpace.bottom);

	// Spawn the installer thread
	ME_CreateThread(ThreadInstall,WindowHWND);

	return true;
}

bool PageInstaller::RenderForm(HWND hWnd)
{
	return	BaseClass::RenderForm(hWnd);
}

LRESULT PageInstaller::WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	if (!Initialized) { return 0; }

	if ( message == WM_COMMAND )
	{
        switch(LOWORD(wParam)) 
        {
            case BN_CLICKED:			
			{
				HWND ClickedButton = (HWND)lParam;
				if (	ClickedButton == cmdNews ||
						ClickedButton == cmdError ||
						ClickedButton == cmdStats )
				{
					ShowWindow(SubStatusLabel,SW_HIDE);
					ShowWindow(txtNews,SW_HIDE);
					ShowWindow(txtError,SW_HIDE);
					if (ClickedButton == cmdNews)
					{
						ShowWindow(txtNews,SW_SHOW);
					}		
					else if (ClickedButton == cmdError)
					{
						ShowWindow(txtError,SW_SHOW);
					}		
					else if (ClickedButton == cmdStats)
					{
						ShowWindow(SubStatusLabel,SW_SHOW);
					}
				}
				break;
			}
			case MSG_SetStatus:
			{			
				SendMessage(StatusLabel,WM_SETTEXT,(WPARAM)lParam,0);
				delete[] (char*)lParam;
			}
			break;
			case MSG_SetSubStatus:
			{
				SendMessage(SubStatusLabel,WM_SETTEXT,(WPARAM)lParam,0);
				delete[] (char*)lParam;
			}
			break;		
			case MSG_SetProgress:
				SetProgress((int)lParam);
			break;
			case MSG_RecieveError:
			{
				char* Error = (char*)lParam;
				MessageBoxA(hWnd,Error,"Maxsi Installer",MB_ICONERROR);
				delete Error;
			}
			break;
			case MSG_RecieveNews:
			{
				char* NewNews = (char*)lParam;
				SetWindowTextA(txtNews,NewNews);
				//SendMessage(txtNews,WM_SETTEXT,(WPARAM)NewNews,0);
				delete NewNews;
			}
			break;
			case GotoFailurePage:
				//ReplaceSelfWith( (MaxsiUI_Form*) new PageFailure );
				break;
			case GotoSuccessPage:
				ReplaceSelfWith( (MaxsiUI_Form*) new PageSuccess );
				break;
			case GotoConnectionPage:
				//ReplaceSelfWith( (MaxsiUI_Form*) new PageConnection );
				break;
			case GotoErrorPage:
				//ReplaceSelfWith( (MaxsiUI_Form*) new PageError );
				break;
			case GotoPasswordPage:
				//ReplaceSelfWith( (MaxsiUI_Form*) new PagePassword );
				break;
			case QuitMessage:
				PostQuitMessage(0);
		}
	}
	else
	{
		return BaseClass::WindowProc(hWnd,message,wParam,lParam);
	}
    return (0);

}


bool PageInstaller::TerminateForm()
{
	// Avoid calling this function twice, or worse,
	// if one of the functions called here invokes another
	// windows message that eventually leads to this point of code again.
	if (!Initialized) { return false; }
	Initialized	=	false;

	DestroyWindow(ProgressBar);
	DestroyWindow(StatusLabel);
	DestroyWindow(SubStatusLabel);
	DestroyWindow(txtNews);
	DestroyWindow(txtError);
	DestroyWindow(cmdNews);
	DestroyWindow(cmdStats);
	DestroyWindow(cmdError);

	// If we are here, we are safe. Call the parent and ask him to continue our work.
	// Make sure he sets Initialized to false afterwards.
	Initialized	=	true;
	BaseClass::TerminateForm();
	return true;
}

bool PageInstaller::NewResolution( WORD Width, WORD Height)
{
	MoveWindow(ProgressBar,25,85,Width-25-25,18,TRUE);
	MoveWindow(StatusLabel,25,55,Width-25-25,85-55,TRUE);
	MoveWindow(SubStatusLabel,25,110,Width-25-25,Height-110-3*24,TRUE);
	MoveWindow(txtNews,25,130,Width-25-25,Height-110-3*24,TRUE);
	MoveWindow(txtError,25,130,Width-25-25,Height-110-3*24,TRUE);
	MoveWindow(cmdStats	,Width-3*125,Height-2*24,100,24,TRUE);
	MoveWindow(cmdNews	,Width-2*125,Height-2*24,100,24,TRUE);
	MoveWindow(cmdError	,Width-1*125,Height-2*24,100,24,TRUE);


	BaseClass::NewResolution(Width,Height);

	return true;
}